import fc.common.R;

setCmd("checkInitFiles", {->
  def args = join($app.projects, ',', {x -> "'$x'"});
  println args;
  def rows = select("""select ns,name,title,is_enabled,vm_extend,vm_extend_file from sys_view 
    where (vm_extend is not null and vm_extend_file is not null) and project in(${args}) order by vm_extend""");
  def vmExtends = [:];
  rows.each({row ->
    vmExtends.put(row.vm_extend,row.vm_extend_file);
    def extFile = row.vm_extend_file;
    if(isEmpty(extFile)) {
      return;
    }
    def file = "./src/extends" + extFile.replace("./","/");
    if(!R.exists(file)) {
      println "gen file: $file";
      R.writeFile(file,"""export default function (meta, vm) {
  return {
    onInit: function () {

    }
  }
}""");
    } else {
      println "$file already exists!";
    }
  });
  fxVmExtends(vmExtends);
});

def fxVmExtends(vmExtends) {
  def extendsFile = "./src/extends/VModel.Extends.js";
  def out = R.getFileWriter(extendsFile, "UTF-8");
  vmExtends.each({kv -> 
    out.println "import ${kv.key} from \"${kv.value}\";";
  });
  out.println "";
  out.print "export default {\r\n  ";
  out.println join(vmExtends.keySet(), ",\r\n  ", k -> k);
  out.println "}";
  out.flush();
  out.close();
  println "$extendsFile updated!";
}